<?php
    defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );

    class Authrule extends Admin_Controller
    {
        public function index()
        {
            #接受参数
            $d = $this->input->get( 'd' );
            if ( $d ) {
                $this->load->model( "admin/navigate_model" );
                $list = $this->navigate_model->getAllList();
                //表单select树形下拉
                if($d=='select'){
                    $list=$this->tree($list);
                    array_unshift($list,['id'=>0,'text'=>'顶级菜单','pid'=>0]);
                    exit(json_encode($list));
                }
                Tool::exitOk($this->tree($list));
            }
            $this->output->cache(1);
            $this->layout->view( "navigate/all" );
        }
       /* public function getData()
        {
            $this->load->model( "admin/navigate_model" );
            $list = $this->navigate_model->getAllList();
            echo json_encode($this->tree($list));exit;
        }*/
        public function tree($table,$p_id='0') {
            $tree = [];
            foreach($table as $row){
                //$row['state']="closed";
                if($row['pid']==$p_id){
                    $tmp = $this->tree($table,$row['id']);
                    if($tmp){
                        $row['children']=$tmp;
                    }else{
                        $row['leaf'] = true;
                    }
                    $tree[]=$row;
                }
            }
            return $tree;
        }

        /**
* 得到阶梯下拉
         */
        public function add()
        {
            $id = $this->input->get( 'id' );
            $this->load->model( "admin/navigate_model" );
            $tree_str = "<select name='pid'>";
            $tree_str .= $this->navigate_model->getTree( $id );
            $tree_str .= "</select>";
            $data[ 'tree_str' ] = $tree_str;
            $data[ 'node' ] = null;
            $this->load->view( "navigate/edit", $data );
        }


        /**
         * 保存
         */
        public function save()
        {
            $get=$this->input->get();

            $this->load->model( "admin/authrule_model" );
            $this->authrule_model->saveRule( $get);
        }

        /**
         * 删除
         */
        public function delete()
        {

        }


    }
